package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.rfid.project.exception.CannotDelException;
import com.mysema.query.BooleanBuilder;

import java.util.List;

@Service
@Transactional
public class CustomerService{

	@Autowired
	private CustomerDao customerDao;
	
	private QCustomer $ = QCustomer.customer;

	@Autowired
	private ProjectDao projectDao;
	
	private QProject $_ = QProject.project;
	
	@Autowired
	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}
	
	@Autowired
	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}
	
	public List<Customer> findAll(){
		return ListUtil.toList(customerDao.findAll());
	}
				
	public List<Customer> findAll(
	String code,
	String name,
	String address,
	String remark,
	String orderByAttribute	){
	BooleanBuilder condition = new BooleanBuilder();
	
	if(StringUtil.isNotEmpty(code)){
		condition.and($.code.contains(code));
	}
	
	if(StringUtil.isNotEmpty(name)){
		condition.and($.name.contains(name));
	}
	
	if(StringUtil.isNotEmpty(address)){
		condition.and($.address.contains(address));
	}
	
	if(StringUtil.isNotEmpty(remark)){
		condition.and($.remark.contains(remark));
	}
	
	
	return ListUtil.toList(customerDao.findAll(condition));
	}		

	public Page<Customer> findAll(
			String code,
			String name,
			String address,
			String remark,
			int pageNumber, int pageSize,String sortAttribute,String orderDirection) {
			BooleanBuilder condition = new BooleanBuilder();
	
			if(StringUtil.isNotEmpty(code)){
				condition.and($.code.contains(code));
			}
			if(StringUtil.isNotEmpty(name)){
				condition.and($.name.contains(name));
			}
			if(StringUtil.isNotEmpty(address)){
				condition.and($.address.contains(address));
			}
			if(StringUtil.isNotEmpty(remark)){
				condition.and($.remark.contains(remark));
			}
	
			Sort sortObject = null;
			if(StringUtil.isEmpty(sortAttribute)){
				sortObject = new Sort(Direction.DESC, "id");
			}else{
				sortObject = new Sort(Direction.fromStringOrNull(orderDirection),sortAttribute);
			}
			
			PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sortObject);
			return customerDao.findAll(condition, pageRequest);
	}
	
	public Customer getCustomer(Long id){
		return customerDao.getOne(id);
	}
	
	public void deleteCustomer(Long id) throws CannotDelException{
		if (this.findProjectByCustomer(id)) {
			throw new CannotDelException("客户信息已经被项目引用，请先删除项目或修改项目的引用！");
		} else {
			customerDao.delete(id);
		}
	}
	
	private boolean findProjectByCustomer(Long customerId){
		BooleanBuilder condition = new BooleanBuilder();
		condition.and($_.customer.id.eq(customerId));
		return this.projectDao.count(condition) > 0;
	}

	public void saveCustomer(Customer customer) {
		customerDao.save(customer);
	}

	public Customer findOne(Long id) {
		return this.customerDao.findOne(id);
	}

}
		
